﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;

namespace YNCNumberSys
{
    public class ZJAppendText
    {
        #region 变量定义
        /// <summary>
        /// 文字的插入点.
        /// </summary>
        private Point3d _pt_Position;
        /// <summary>
        /// 文字的对其点.
        /// </summary>
        private Point3d _pt_AlignmentPoint;
        /// <summary>
        /// 文字水平对齐方式.
        /// </summary>
        private TextHorizontalMode _mode_HorizontalMode;
        /// <summary>
        /// 文字垂直对齐方式.
        /// </summary>
        private TextVerticalMode _mode_VerticalMode;
        /// <summary>
        /// 文字内容.
        /// </summary>
        private string _str_TextString;
        /// <summary>
        /// 字高
        /// </summary>
        private double _d_Height;
        /// <summary>
        /// 文字方向
        /// </summary>
        private double _d_Rotation;
        /// <summary>
        /// 文字宽度比例
        /// </summary>
        private double _d_WidthFactor;
        /// <summary>
        /// 插入文字的ID,返回结果值.
        /// </summary>
        private ObjectId _id_DBText;
        /// <summary>
        /// 文字样式的对象标识符号.
        /// </summary>
        private ObjectId _id_TextStyleTableRecord;
        /// <summary>
        /// 文字样式的名称.
        /// </summary>
        string _str_TextStyleName;
        /// <summary>
        /// 事务,为空表示操作当前图纸空间.
        /// </summary>
        private Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase和图纸文档均为当前图纸空间,并且图纸文档未锁定;
        /// (2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;
        #endregion

        public ObjectId id_DBText
        {
            get { return _id_DBText; }
            set { _id_DBText = value; }
        }

        public ZJAppendText(ACADDatabase wdb_DataBase, Transaction trs_Transaction,
            Point3d pt_Position, Point3d pt_AlignmentPoint, string str_TextString, string str_TextStyleName, TextHorizontalMode mode_HorizontalMode = TextHorizontalMode.TextCenter,
            TextVerticalMode mode_VerticalMode = TextVerticalMode.TextVerticalMid, double d_Height = 3.5, double d_Rotation = 0.0, double d_WidthFactor=1.0)
        {
            _wdb_DataBase = wdb_DataBase;
            _trs_Transaction = trs_Transaction;

            _pt_Position = pt_Position;
            _pt_AlignmentPoint = pt_AlignmentPoint;
            _mode_HorizontalMode = mode_HorizontalMode;
            _mode_VerticalMode = mode_VerticalMode;
            _str_TextString = str_TextString;
            _str_TextStyleName = str_TextStyleName;
            _d_Height = d_Height;
            _d_Rotation = d_Rotation;
            _d_WidthFactor = d_WidthFactor;
            _id_DBText = ObjectId.Null;
            _id_TextStyleTableRecord = ObjectId.Null;
            _b_TransactionIsUsed = true;
        }

        public ZJAppendText(
            Point3d pt_Position, Point3d pt_AlignmentPoint, string str_TextString, string str_TextStyleName, TextHorizontalMode mode_HorizontalMode = TextHorizontalMode.TextCenter,
            TextVerticalMode mode_VerticalMode = TextVerticalMode.TextVerticalMid, double d_Height = 3.5, double d_Rotation = 0.0, double d_WidthFactor = 1.0)
        {
            _wdb_DataBase = HostApplicationServices.WorkingDatabase; ;
            _trs_Transaction = null;
            _pt_Position = pt_Position;
            _pt_AlignmentPoint = pt_AlignmentPoint;
            _mode_HorizontalMode = mode_HorizontalMode;
            _mode_VerticalMode = mode_VerticalMode;
            _str_TextString = str_TextString;
            _d_Height = d_Height;
            _d_Rotation = d_Rotation;
            _d_WidthFactor=d_WidthFactor;
            _id_DBText = ObjectId.Null;
            _id_TextStyleTableRecord = ObjectId.Null;
            _b_TransactionIsUsed = false;
        }

        private void DoAppendText()
        {
            BlockTable bt = (BlockTable)_trs_Transaction.GetObject(_wdb_DataBase.BlockTableId, OpenMode.ForWrite);
            BlockTableRecord pMRecord = (BlockTableRecord)_trs_Transaction.GetObject(_wdb_DataBase.CurrentSpaceId, OpenMode.ForWrite);
            
            DBText db_Text = new DBText();
            db_Text.SetDatabaseDefaults();
            db_Text.Position = _pt_Position;
            db_Text.TextString = _str_TextString;
            ZJGetTextStyleTableRecord cls_ZJGetTextStyleTableRecord = new ZJGetTextStyleTableRecord("_TCH_DIM");
            if (cls_ZJGetTextStyleTableRecord.Has())
            {
                db_Text.TextStyleId = cls_ZJGetTextStyleTableRecord.Do();
            }

            db_Text.Height = _d_Height;
            db_Text.Rotation = _d_Rotation;
            db_Text.WidthFactor = _d_WidthFactor;
            db_Text.HorizontalMode = _mode_HorizontalMode;
            db_Text.VerticalMode = _mode_VerticalMode;
            db_Text.AlignmentPoint = _pt_AlignmentPoint;

            pMRecord.AppendEntity(db_Text);
            _trs_Transaction.AddNewlyCreatedDBObject(db_Text, true);
            _id_DBText = db_Text.ObjectId;
        }

        public ObjectId Do()
        {
            _id_DBText = ObjectId.Null;
            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    DoAppendText();
                    _trs_Transaction.Commit();
                    _trs_Transaction.Dispose();
                }
            }
            else
            {
                DoAppendText();
            }
            return _id_DBText;
        }
    }
}
